﻿<template>
    <div class="UserDetail">
        <MyDialog :model-value="true" :type="DialogType.C1" :title="title" @close="onClose">
            <el-form ref="UserDetailFormRef" label-width="80" :model="thisData" :rules="rules">
                <el-form-item label="登录名" prop="loginName">
                    <el-input v-model="thisData.loginName" maxlength="50" :disabled="rowAutoID == null ? false : true"></el-input>
                </el-form-item>

                <el-form-item label="登录密码" prop="loginPassword" v-if="rowAutoID == null">
                    <el-input v-model="thisData.loginPassword" maxlength="200"></el-input>
                </el-form-item>

                <el-form-item label="姓名" prop="name">
                    <el-input v-model="thisData.name" maxlength="50"></el-input>
                </el-form-item>

                <el-form-item label="角色" prop="roleIDs">
                    <DataSelect class="w-100" v-model:id="thisData.roleIDs" table="Role" multiple collapse-tags></DataSelect>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="thisData.email" maxlength="30"></el-input>
                </el-form-item>

                <el-form-item label="手机号" prop="phone">
                    <el-input v-model="thisData.phone" maxlength="11"></el-input>
                </el-form-item>

                <el-form-item label="数据归属" prop="selectDepartmentIDs">
                    <DepartmentIds v-model="thisData.selectDepartmentIDs"></DepartmentIds>
                </el-form-item>
            </el-form>

            <template #footer>
                <el-button type="primary" :loading="loading" @click="onSave">保存</el-button>
            </template>
        </MyDialog>
    </div>
</template>

<script lang="ts" setup>
import Enums from "@/gx/Enums";
import Gx from "@/gx/Gx";
import { FormRules } from "element-plus";
import { DialogType, TipsType } from "@/_gx/TsEnums";
import { tokenStore } from "@/stores/tokenStore";

//接收父组件的传值
const rowAutoID = inject("rowAutoID") as Ref<number | null>;

//定义当前页面数据对象
const thisData = ref<any>({
    roleIDs: [],
    selectDepartmentIDs: [-1],
});

const oldLoginName = ref(null);

//loading...
const loading = ref(false);

//定义表单验证规则
const rules = ref<FormRules>({
    loginName: [
        { required: true, message: "请输入登录名", trigger: "blur" },
        { validator: CusFormCheck.DBHas("User", "登录名", "_LoginNameEqual").bind(null, oldLoginName), trigger: "blur" },
    ],
    loginPassword: [{ required: true, message: "请输入登录密码", trigger: "blur" }],
    name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
    roleIDs: [{ required: true, message: "请选择角色", trigger: "blur" }],
    selectDepartmentIDs: [{ required: true, message: "请选择数据归属", trigger: "blur" }],
});

//标题
const title = computed(() => {
    if (Gx.IsEmpty(rowAutoID.value)) {
        return "用户详情 - 创建";
    } else {
        return `用户详情 - 编辑 - ${thisData.value.loginName}`;
    }
});

//初始化
const Init = () => {
    if (!Gx.IsEmpty(rowAutoID.value)) {
        App.CallApi(Api.User.GetByID(rowAutoID.value), {
            success: (res) => {
                oldLoginName.value = res.loginName;
                thisData.value = res;
            },
        });
    }
};

//表单Ref对象
const UserDetailFormRef = ref(null);

//保存
const onSave = () => {
    UserDetailFormRef.value.validate((isOk: boolean) => {
        if (!isOk) {
            return;
        }

        let _api = null;
        let _data = thisData.value;
        if (Gx.IsEmpty(rowAutoID.value)) {
            //添加
            _api = Api.User.Insert;
        } else {
            //修改
            _api = Api.User.Update;
        }

        loading.value = true;
        App.CallApi(_api, {
            data: _data,
            success: (res) => {
                if (!Gx.IsEmpty(rowAutoID.value)) {
                    const store = tokenStore();
                    if (store.me.userID == rowAutoID.value) {
                        App.Alert({
                            msg: "保存成功，但需要重新登录！",
                            type: TipsType.Success,
                            onOk: () => {
                                App.Logout();
                            },
                        });
                    }
                }

                emits("close", true);
            },
            complete: () => {
                loading.value = false;
            },
        });
    });
};

//关闭
const onClose = () => {
    emits("close", false);
};

//向上抛出的事件与方法
const emits = defineEmits(["close"]);
defineExpose({ Init });
</script>
